package com.fengye.serialization.common;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fengye.common.CommonUtil;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;

import java.io.IOException;
import java.util.Date;

/**
 * @Author fengye
 * @Date 2023/2/13 9:48
 **/
public class FDateJsonDeserializer extends JsonDeserializer<Date> {

    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        String date = jsonParser.getText();
        if(StringUtils.isEmpty(date)) {
            return null;
        }

        if(StringUtils.isNumeric(date)) {
            return new Date(Long.valueOf(date));
        }

        DateTime dateTime = CommonUtil.DATE_TIME_FORMATTER.parseDateTime(date);
        return dateTime.toDate();
    }
}
